package com.example.glutil;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.opengl.GLES20;
import android.opengl.GLUtils;

import java.io.IOException;
import java.io.InputStream;

/**
 * This class is designed to provide utility functions regarding the opengl Texture
 */
public class TextureHelper
{
    /**
     * Load a texture of an opengl object and return the resource id of the loaded texture
     * @param context the context of the application
     * @param resourceId the resource id of the texture
     * @param text the text to add to the texture. If the text is null no text is added
     * @return
     */
	public static int loadTexture(final Context context, final int resourceId, final String text)
	{
        // create identifier
        int mTextureID;
        int[] textures = new int[1];
        GLES20.glGenTextures(1, textures, 0);
        // get the identifier
        mTextureID = textures[0];
        // bind the identifier
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureID);

        GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER,GLES20.GL_NEAREST);
        GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,GLES20.GL_TEXTURE_MAG_FILTER,GLES20.GL_LINEAR);

        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_REPEAT);
        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_REPEAT);

        InputStream is = context.getResources().openRawResource(resourceId);
        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inPreferredConfig = Bitmap.Config.ARGB_8888;
        Bitmap bitmap;

        try {
            bitmap = BitmapFactory.decodeStream(is, null, opts);

        } finally {
            try {
                is.close();
            } catch(IOException e) {
                // Ignore.
            }
        }
        if (text != null){
            bitmap = addText(text, bitmap);
        }

        GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
        bitmap.recycle();
        return mTextureID;
	}

    /**
     * Add a text to the texture
     * @param value the text to add
     * @param bm the bitmap of the current texture to add the text in
     * @return
     */
    private static Bitmap addText(String value, Bitmap bm){
        int fontSize = 40;
        if (value.length() < 12){
            fontSize += (12/value.length())*3;
        }
        Bitmap alteredBitmap = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), bm.getConfig());
        Canvas canvas = new Canvas(alteredBitmap);
        Paint paint = new Paint();
        canvas.drawBitmap(bm, 0, 0, paint);
        paint.setColor(Color.BLACK);
        paint.setTextSize(fontSize);
        paint.setAntiAlias(true);
        paint.setTextAlign(Paint.Align.CENTER);
        int xPos = (canvas.getWidth() / 2);
        int yPos = (int) ((canvas.getHeight() / 2) - ((paint.descent() + paint.ascent()) / 2)) ;

        canvas.drawText(value, xPos, yPos, paint);

        return alteredBitmap;
    }


}
